home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / AlternateBufferDisplay / AlternateBufferDisplay.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  8.3 KB  |  329 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        AlternateBufferDisplay.                                                */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    Shows that copybits along with offscreen bitmaps    */
  7. /*            can emulate alternating buffer displays at a reasonable rate.    */
  8. /*                                                                            */
  9. /*        Version:        1.0 Completed 11/12/91                                */
  10. /*                                                                            */
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. #include    <QDOffscreen.h>
  14.  
  15. #define Gestalttest        0xA1AD
  16. #define NoTrap            0xA89F
  17.  
  18. #define    appleID            128            
  19. #define    appleMenu        0
  20. #define    aboutMeCommand    1
  21.  
  22. #define    fileID            129
  23. #define    quitCommand     1
  24.  
  25. #define    aboutMeDLOG        128
  26. #define    okButton        1
  27.  
  28. /*------------------------------------------------------*/
  29. /*    Global Variables.                                    */
  30. /*------------------------------------------------------*/
  31.  
  32. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  33. WindowPtr            myWindow;
  34. Boolean                DoneFlag;
  35. MenuHandle            mymenu0, mymenu1;
  36. GWorldPtr            offscreenGWorld1, offscreenGWorld2;
  37.  
  38. /*------------------------------------------------------*/
  39. /*    showAboutMeDialog().                                */
  40. /*------------------------------------------------------*/
  41.  
  42. void showAboutMeDialog()
  43. {
  44.     GrafPtr     savePort;
  45.     DialogPtr    theDialog;
  46.     short        itemHit;
  47.  
  48.     GetPort(&savePort);
  49.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  50.     SetPort(theDialog);
  51.  
  52.     do {
  53.         ModalDialog(nil, &itemHit);
  54.     } while (itemHit != okButton);
  55.  
  56.     CloseDialog(theDialog);
  57.  
  58.     SetPort(savePort);
  59.     return;
  60. }
  61.  
  62. /*------------------------------------------------------*/
  63. /*    init().                                                */
  64. /*------------------------------------------------------*/
  65.  
  66. void init()
  67. {
  68.     RgnHandle            tempRgn;
  69.     Rect                BaseRect;
  70.     OSErr                err;
  71.     GDHandle            SaveGD;
  72.     CGrafPtr            SavePort;
  73.     RGBColor            black = {0,0,0}, white = {65535,65535,65535};
  74.  
  75.     InitGraf(&qd.thePort);
  76.     FlushEvents(everyEvent, 0);
  77.     InitWindows();
  78.     InitDialogs(nil);
  79.     InitCursor();
  80.  
  81.     /*    Set up menus.    */
  82.     mymenu0 = GetMenu(appleID);
  83.     AddResMenu(mymenu0, 'DRVR');
  84.     InsertMenu(mymenu0,0);
  85.     mymenu1 = GetMenu(fileID);
  86.     InsertMenu(mymenu1,0);
  87.     DrawMenuBar();
  88.  
  89.     /* Main initialization.    */
  90.     DoneFlag = FALSE;
  91.  
  92.     /*    Set Rects.    */
  93.     SetRect(&BaseRect, 40, 60, 490, 540);
  94.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-56, 
  95.                 BaseRect.bottom - 76);
  96.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  97.                             WinMinusScroll.right, WinMinusScroll.bottom);
  98.     tempRgn = GetGrayRgn();
  99.     HLock ((Handle) tempRgn);
  100.     TotalRect = (**tempRgn).rgnBBox;
  101.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  102.                 (**tempRgn).rgnBBox.bottom - 40);
  103.     HUnlock ((Handle) tempRgn);
  104.  
  105.     /*    Open window and set up picture.    */
  106.     GetGWorld (&SavePort, &SaveGD);
  107.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  108.                             (WindowPtr) -1, TRUE, 150);
  109.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  110.     DrawGrowIcon (myWindow);
  111.  
  112.     GetGWorld (&SavePort, &SaveGD);
  113.     
  114.     /*    Make first offscreen buffer.    */
  115.     if (NewGWorld (&offscreenGWorld1, 0, &InitWindowSize, nil, nil, 0))
  116.         Debugger();
  117.     SetGWorld (offscreenGWorld1, nil);
  118.     RGBForeColor(&white);
  119.     PaintRect (&InitWindowSize);
  120.     
  121.     /*    Make second offscreen buffer.    */
  122.     if (NewGWorld (&offscreenGWorld2, 0, &InitWindowSize, nil, nil, 0))
  123.         Debugger();
  124.     SetGWorld (offscreenGWorld2, nil);
  125.     RGBForeColor(&black);
  126.     PaintRect (&InitWindowSize);
  127.     
  128.     SetGWorld (SavePort, SaveGD);
  129. }
  130.  
  131. /*------------------------------------------------------*/
  132. /*    doCommand().                                        */
  133. /*------------------------------------------------------*/
  134.  
  135. void doCommand(mResult)
  136.     long    mResult;
  137. {
  138.     int                     theMenu, theItem;
  139.     char                    daName[256];
  140.     GrafPtr                 savePort;
  141.  
  142.     theItem = LoWord(mResult);
  143.     theMenu = HiWord(mResult);
  144.     
  145.     switch (theMenu) {
  146.         case appleID:
  147.             if (theItem == aboutMeCommand)
  148.                 showAboutMeDialog();
  149.             else {
  150.                 GetItem(mymenu0, theItem, daName);
  151.                 GetPort(&savePort);
  152.                 (void) OpenDeskAcc(daName);
  153.                 SetPort(savePort);
  154.             }
  155.             break;
  156.  
  157.         case fileID:
  158.             switch (theItem) {
  159.                 case quitCommand:
  160.                     DoneFlag = TRUE;
  161.                     break;
  162.                 default:
  163.                     break;
  164.                 }
  165.             break;
  166.     }
  167.     HiliteMenu(0);
  168.     return;
  169. }
  170.  
  171. /*------------------------------------------------------*/
  172. /*    draw().                                                */
  173. /*------------------------------------------------------*/
  174.  
  175. void draw()
  176. {
  177.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  178.     long            delay;
  179.     char            theString[255];
  180.     GDHandle        screensDevice;
  181.     Rect            area;
  182.     int                i;
  183.         
  184.     RGBForeColor (&black);
  185.     RGBBackColor (&White);
  186.     EraseRect(&WinMinusScroll);
  187.     
  188.  
  189.     area = WinMinusScroll;
  190.     LocalToGlobal(&topLeft(area));
  191.     LocalToGlobal(&botRight(area));
  192.     screensDevice = GetMaxDevice(&area);
  193.     if (screensDevice != nil) {
  194.         (**(**offscreenGWorld1->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  195.         (**(**offscreenGWorld2->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  196.     }
  197.     delay = TickCount();
  198.     for (i=0; i< 10; i++) {
  199.         CopyBits ((BitMap *) *offscreenGWorld2->portPixMap, &myWindow->portBits, 
  200.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  201.         CopyBits ((BitMap *) *offscreenGWorld1->portPixMap, &myWindow->portBits, 
  202.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  203.     }
  204.     delay = TickCount() - delay;
  205.     MoveTo(50,50);
  206.     NumToString(delay, theString);
  207.     DrawString(theString);
  208. }
  209.  
  210. /*------------------------------------------------------*/
  211. /*    main().                                                */
  212. /*------------------------------------------------------*/
  213.  
  214. main()
  215. {
  216.     char            key;
  217.     Boolean            track;
  218.     long            growResult;
  219.     EventRecord     myEvent;
  220.     WindowPtr        whichWindow;
  221.     int                yieldTime;
  222.  
  223.  
  224.     init();
  225.     yieldTime = 0;
  226.     for ( ;; ) {
  227.         if (DoneFlag)
  228.             ExitToShell();
  229.             
  230.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  231.             switch (myEvent.what) {
  232.                 case mouseDown:
  233.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  234.                         case inSysWindow:
  235.                             SystemClick(&myEvent, whichWindow);
  236.                             break;
  237.                         case inMenuBar:
  238.                             doCommand(MenuSelect(myEvent.where));
  239.                             break;
  240.                         case inContent:
  241.                             break;
  242.                         case inDrag:
  243.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  244.                             draw();
  245.                             DrawGrowIcon (whichWindow);
  246.                             break;
  247.                         case inGrow:
  248.                             growResult = GrowWindow (whichWindow, myEvent.where,
  249.                                                     &minRect);
  250.                             SizeWindow(whichWindow, LoWord(growResult), 
  251.                                     HiWord(growResult), TRUE);
  252.                             EraseRect(&whichWindow->portRect);
  253.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  254.                                     whichWindow->portRect.top, 
  255.                                     whichWindow->portRect.right-20, 
  256.                                     whichWindow->portRect.bottom - 20);
  257.                             draw();
  258.                             DrawGrowIcon (whichWindow);
  259.                             break;
  260.                         case inGoAway:
  261.                             track = TrackGoAway (whichWindow, myEvent.where);
  262.                             if (track) {
  263.                                 CloseWindow (whichWindow);
  264.                                 DoneFlag = TRUE;
  265.                                 }
  266.                             break;
  267.                         case inZoomIn:
  268.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  269.                             if (track) {
  270.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  271.                                 EraseRect(&whichWindow->portRect);
  272.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  273.                                         whichWindow->portRect.top, 
  274.                                         whichWindow->portRect.right-20, 
  275.                                         whichWindow->portRect.bottom - 20);
  276.                                 draw();
  277.                                 DrawGrowIcon (whichWindow);
  278.                                 }
  279.                             break;
  280.                         case inZoomOut:
  281.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  282.                             if (track) {
  283.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  284.                                 EraseRect(&whichWindow->portRect);
  285.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  286.                                         whichWindow->portRect.top, 
  287.                                         whichWindow->portRect.right-20, 
  288.                                         whichWindow->portRect.bottom - 20);
  289.                                 draw();
  290.                                 DrawGrowIcon (whichWindow);
  291.                                 }
  292.                             break;
  293.                         default:
  294.                             break;
  295.                         }
  296.                     break;
  297.                 case keyDown:
  298.                 case autoKey:
  299.                     key = myEvent.message & charCodeMask;
  300.                     if ( myEvent.modifiers & cmdKey )
  301.                         if ( myEvent.what == keyDown )
  302.                             doCommand(MenuKey(key));
  303.                     break;
  304.                 case updateEvt:
  305.                     if ((WindowPtr) myEvent.message == myWindow) {
  306.                         BeginUpdate((WindowPtr) myWindow);
  307.                         EndUpdate((WindowPtr) myWindow);
  308.                         draw();
  309.                         }
  310.                     break;
  311.                 case diskEvt:
  312.                     break;
  313.                 case activateEvt:
  314.                     break;
  315.                 case app4Evt:
  316.                     if ((myEvent.message << 31) == 0) {
  317.                         yieldTime = 30;
  318.                         }
  319.                     else {
  320.                         yieldTime = 0;
  321.                         SetPort((WindowPtr) myWindow);
  322.                         }
  323.                     break; 
  324.                 default:
  325.                     break;
  326.                 }
  327.             }
  328.         }
  329. }